<?php

@session_start();

extract($_REQUEST);

require_once "../lib/config.php";
require_once "../lib/functions.php";
require_once "../lib/pdomanager.php";
require_once "../lib/paginate_class.php";

$pdodbobj=new DB();
$pdodbobj->dbname=DBNAME;
$pdodbobj->hostname=HOST;
$pdodbobj->dbusername=USERNAME;
$pdodbobj->dbpassword=PASSWORD;
$pdodbobj->dbtype=TYPE;
$pdodbobj->portnumber=PORTNUMBER;

$starcount=1;

$connstatus=$pdodbobj->connect();

if($action=="saverating" && $userid>0 && $starrating>0 && $appid>0)
{
    $selecquery="select id from af_entertainment_ratings where entertainment_id=:appid and user_id=:userid ";
    $existsrecord= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid,":userid"=>$userid)));
    if(count($existsrecord)>0)
    {
        echo "Already";
    }
    else
    {
        $insertquery="insert into af_entertainment_ratings set entertainment_id=:appid,user_id=:userid,rating=:rating,rate_date='".time()."' ";
        $squeryobj=$pdodbobj->query($insertquery,array(":appid"=>$appid,":userid"=>$userid,":rating"=>$starrating));
        
        
        if($pdodbobj->affectedrows>0)
        {
            $selecquery="select avg(rating) as avgrating from af_entertainment_ratings where entertainment_id=:appid ";
            $averagerating= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid)));
            if(count($averagerating)>0)
            {
                $updatequery="update af_entertainments set avg_rating='".$averagerating[0]['avgrating']."' where id=:appid";
                $pdodbobj->query($updatequery,array(":appid"=>$appid));
                echo round($averagerating[0]['avgrating']);
            }
        }
    }
}

if($action=="pickupdate" && $userid>0 && $appid>0)
{
    $selecquery="select id from af_entertainment_pics where entertainment_id=:appid and user_id=:userid ";
    $existsrecord= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid,":userid"=>$userid)));
    if(count($existsrecord)>0)
    {
        $pdodbobj->query("delete from af_entertainment_pics where entertainment_id=:appid and user_id=:userid",array(":appid"=>$appid,":userid"=>$userid));
        
        $updatequery="update af_entertainments set total_pics=total_pics-1 where id=:appid";
        $pdodbobj->query($updatequery,array(":appid"=>$appid));
        
        $selecquery="select total_pics from af_entertainments where id=:appid ";
        $pickcount= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid)));
        
        echo $pickcount[0]['total_pics'];
        
    }
    else
    {
        $insertquery="insert into af_entertainment_pics set entertainment_id=:appid,user_id=:userid,pic_date='".time()."' ";
        $pdodbobj->query($insertquery,array(":appid"=>$appid,":userid"=>$userid));
        if($pdodbobj->affectedrows>0)
        {
            
            $updatequery="update af_entertainments set total_pics=total_pics+1 where id=:appid";
            $pdodbobj->query($updatequery,array(":appid"=>$appid));
            
            $selecquery="select total_pics from af_entertainments where id=:appid ";
            $pickcount= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid)));
            
            echo $pickcount[0]['total_pics'];        
        }
    }
    /*
    $insertquery="insert into af_entertainment_pics set entertainment_id=:appid,user_id=:userid,pic_date='".time()."' ";
    $pdodbobj->query($insertquery,array(":appid"=>$appid,":userid"=>$userid));
    if($pdodbobj->affectedrows>0)
    {
        $updatequery="update af_entertainments set total_pics=total_pics+1 where id=:appid";
        $pdodbobj->query($updatequery,array(":appid"=>$appid));
        
        $selecquery="select total_pics from af_entertainments where id=:appid ";
        $pickcount= $pdodbobj->fetch_array($pdodbobj->query($selecquery,array(":appid"=>$appid)));
        
        echo $pickcount[0]['total_pics'];
    }
    */
}


if($action=="getapps" && $nextcount>0 )
{
    $condition=array();
    
    $query="select id,title,image_url,link,small_description,total_pics,avg_rating from af_entertainments where status='Active' ";
    $serachcond="";
    
    if($type=='f')
    {
        $query=$query." and type='FM' ";
    }
    if($type=='m')
    {
        $query=$query." and type='Movie' ";
    }
    if($type=='t')
    {
        $query=$query." and type='TV' ";
    }
    
    if(isset($istrend) && $istrend>0)
    {
        $query.=" order by total_pics desc limit ".($currentcount-1).",".$nextcount;
    }
    else
    {
        $query.=" order by added_date desc limit ".($currentcount-1).",".$nextcount;
    }
    $entlinksinfo=$pdodbobj->fetch_array($pdodbobj->query($query,$condition));
    $starcount=$currentcount;
    if(count($entlinksinfo)>0)
    {
        for($j=0;$j<count($entlinksinfo);$j++)
        {
            if(!(strstr($entlinksinfo[$j]['link'],"http://")) && trim($entlinksinfo[$j]['link'])!='')
            {
                $entlinksinfo[$j]['link']="http://".$entlinksinfo[$i]['link'];
            }
            
            if($type=="m" || $type=="t")
            {
                ?>
                <tr>
                    <td style="padding-left: 20px;padding-right:20px;padding:10px auto;height:30px;">
                        <div style="border-top: 1px solid #999;"></div>
                    </td>
                    
                </tr>
                <tr>
                    <td>
                        <div class="chanel_pic">
                            <a href="javascript:void(0);" <?php  if(trim($entlinksinfo[$j]['link'])!='http://'){ ?> onclick="window.open('<?php echo $entlinksinfo[$j]['link']; ?>');" <?php } ?> >
                                <img src="<?php echo "uploads/entertainments/".$entlinksinfo[$j]['image_url']; ?>" width="166px" height="116px" />
                            </a>
                    	</div>
                		<div class="clearfix"></div>
                		<div <?php if($type=="t"){ ?> class="star2" style="padding-right: 5px;float:right;" <?php }else{ ?> class="star" style="padding-left: 5px;" <?php } ?> >
    						<p class="ch_name"><a href="javascript:void(0);" <?php  if(trim($entlinksinfo[$j]['link'])!='http://'){ ?> onclick="window.open('<?php echo $entlinksinfo[$j]['link']; ?>');" <?php } ?> ><?php echo $entlinksinfo[$j]['title']; ?></a></p>
    						<p class="ret">Rating<div id="star_<?php echo $type ?>_<?php echo $starcount; ?>"></div></p>
                            <input type="hidden" name="entertainid_<?php echo $type ?>_<?php echo $starcount; ?>" id="entertainid_<?php echo $type ?>_<?php echo $starcount; ?>" value="<?php echo $entlinksinfo[$j]['id']; ?>" />
                            <input type="hidden" name="avgrating_<?php echo $type ?>_<?php echo $starcount; ?>" id="avgrating_<?php echo $type ?>_<?php echo $starcount; ?>" value="<?php echo round($entlinksinfo[$j]['avg_rating']); ?>" />
                            <p class="ret"><a href="javascript:void(0);" onclick="updatePickcounter(<?php echo $entlinksinfo[$j]['id']; ?>,<?php if(isset($userid) && $userid>0){ echo $userid; }else{ echo "0"; } ?>,'<?php echo $entlinksinfo[$j]['link']; ?>',<?php echo $starcount; ?>,'<?php echo $type ?>');">pick</a><span class="num" id="pick_<?php echo $type ?>_<?php echo $starcount; ?>"><?php echo $entlinksinfo[$j]['total_pics']; ?></span></p>
    						<p class="ret"><?php echo $entlinksinfo[$j]['small_description']; ?></p>
    						
    					</div>
                    </td>
                </tr>
                <?php
            }
            else
            {
                if(($j)%2==0)
                {
                    $class1="2";
                    $class2="";
                }
                if(($j)%4==0)
                {
                    $class1='';
                    $class2='2';
                }
                    ?>
                    <tr>
                        <td style="padding-left: 20px;padding-right:20px;padding:10px auto;height:30px;">
                            <div style="border-top: 1px solid #999;"></div>
                        </td>
                        <td style="padding-left: 20px;padding-right:20px;padding:10px auto;">
                            <div style="border-top: 1px solid #999;"></div>
                        </td>
                    </tr>
                        <tr>
    					<td style="border-right:1px solid #ccebef" >
    						<div class="chanel_pic<?php echo $class1; ?>">
                                <a href="javascript:void(0);" <?php  if(trim($entlinksinfo[$j]['link'])!='http://'){ ?> onclick="window.open('<?php echo $entlinksinfo[$j]['link']; ?>');" <?php } ?> >
                                    <img src="<?php echo "uploads/entertainments/".$entlinksinfo[$j]['image_url']; ?>" width="166px" height="116px" />
                                </a>
    						</div>
    						<div class="star<?php echo $class1; ?>" <?php if($class1==2){ echo "style='float:right;'"; } ?> >
    						<p class="ch_name"><a href="javascript:void(0);" <?php  if(trim($entlinksinfo[$j]['link'])!='http://'){ ?> onclick="window.open('<?php echo $entlinksinfo[$j]['link']; ?>');" <?php } ?> ><?php echo $entlinksinfo[$j]['title']; ?></a></p>
    						<p class="ret">Rating<div id="star_<?php echo $type ?>_<?php echo $starcount; ?>"></div></p>
                                 <input type="hidden" name="entertainid_<?php echo $type ?>_<?php echo $starcount; ?>" id="entertainid_<?php echo $type ?>_<?php echo $starcount; ?>" value="<?php echo $entlinksinfo[$j]['id']; ?>" />
                                 <input type="hidden" name="avgrating_<?php echo $type ?>_<?php echo $starcount; ?>" id="avgrating_<?php echo $type ?>_<?php echo $starcount; ?>" value="<?php echo round($entlinksinfo[$j]['avg_rating']); ?>" />
    						<p class="ret"><a href="javascript:void(0);" onclick="updatePickcounter(<?php echo $entlinksinfo[$j]['id']; ?>,<?php if(isset($userid) && $userid>0){ echo $userid; }else{ echo "0"; } ?>,'<?php echo $entlinksinfo[$j]['link']; ?>',<?php echo $starcount; ?>,'<?php echo $type ?>');">pick</a><span class="num" id="pick_<?php echo $type ?>_<?php echo $starcount; ?>"><?php echo $entlinksinfo[$j]['total_pics']; ?></span></p>
    						<p class="ret"><?php //echo $entlinksinfo[$j]['small_description']; ?></p>
    						
    						</div>	
    						<div class="clearfix"></div>
    						<span class="out" style="float: <?php if($class1==2){ echo "right;padding-right:5px;"; }else{ echo "left"; } ?>;"><?php echo $entlinksinfo[$j]['small_description']; ?></span>
    					</td>
                        <?php
                        $starcount++;
                        $j++;
                        if($j<count($entlinksinfo))
                        {
                        ?>
    					<td>
    						<div class="chanel_pic<?php echo $class2; ?>">
    						<a href="javascript:void(0);" <?php  if(trim($entlinksinfo[$j]['link'])!='http://'){ ?> onclick="window.open('<?php echo $entlinksinfo[$j]['link']; ?>');" <?php } ?> >
                                    <img src="<?php echo "uploads/entertainments/".$entlinksinfo[$j]['image_url']; ?>" width="166px" height="116px" />
                                </a>
    						</div>
    						<div class="star<?php echo $class2; ?>" <?php if($class2==2){ echo "style='float:right;'"; } ?> >
    						<p class="ch_name"><a href="javascript:void(0);" <?php  if(trim($entlinksinfo[$j]['link'])!='http://'){ ?> onclick="window.open('<?php echo $entlinksinfo[$j]['link']; ?>');" <?php } ?> ><?php echo $entlinksinfo[$j]['title']; ?></a></p>
    						<p class="ret">Rating<div id="star_<?php echo $type ?>_<?php echo $starcount; ?>"></div></p>
                                 <input type="hidden" name="entertainid_<?php echo $type ?>_<?php echo $starcount; ?>" id="entertainid_<?php echo $type ?>_<?php echo $starcount; ?>" value="<?php echo $entlinksinfo[$j]['id']; ?>" />
                                 <input type="hidden" name="avgrating_<?php echo $type ?>_<?php echo $starcount; ?>" id="avgrating_<?php echo $type ?>_<?php echo $starcount; ?>" value="<?php echo round($entlinksinfo[$j]['avg_rating']); ?>" />
    						<p class="ret"><a href="javascript:void(0);" onclick="updatePickcounter(<?php echo $entlinksinfo[$j]['id']; ?>,<?php if(isset($userid) && $userid>0){ echo $userid; }else{ echo "0"; } ?>,'<?php echo $entlinksinfo[$j]['link']; ?>',<?php echo $starcount; ?>,'<?php echo $type ?>');">pick</a><span class="num" id="pick_<?php echo $type ?>_<?php echo $starcount; ?>"><?php echo $entlinksinfo[$j]['total_pics']; ?></span></p>
    						<p class="ret"><?php //echo $entlinksinfo[$j]['small_description']; ?></p>
    						
    						</div>	
    						<div class="clearfix"></div>
    						<span class="out" style="float: <?php if($class2==2){ echo "right;padding-right:5px;"; }else{ echo "left"; } ?>;"><?php echo $entlinksinfo[$j]['small_description']; ?></span>                            					</td>
                        </td>
                        <?php
                        }
                        
                        ?>     
    				</tr>
                 <?php   
                
                
            }
            $starcount++;    
        } 
        echo "~:~".$starcount;
    }
    else
    {
        if(isset($newflag) && $newflag==1)
        {
            ?>
            <tr>
                <td valign="top" <?php if($type=="f"){ echo "colspan='2'"; } ?>>
                    <span style="color: red;">
                        No Link Found...
                    </span>
                </td>
            </tr>
            <?php
            echo "~:~0";
        }
    }             
}

?>